<?php
// vim: set ts=4 sw=4 sts=4 et ft=php:

/**
 * @file
 * General module functionality
 *
 * @category  Litecommerce connector
 * @package   Litecommerce connector
 * @author    Creative Development LLC <info@cdev.ru>
 * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
 * @link      http://www.litecommerce.com/
 * @since     1.0.0
 */

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', TRUE);

/**
 * Module initialization
 *
 * @hook   init
 * @return void
 * @since  1.0.0
 */
function lc_connector_init() {
    return LCConnector_Handler::callSafely('Module', 'invokeHookInit');
}

/**
 * Navigation menu access point declaration
 *
 * @hook   menu
 * @return array
 * @since  1.0.0
 */
function lc_connector_menu() {
    return array(
        'admin/modules/lc_connector' => array(
            'title'            => 'LC Connector',
            'description'      => 'Settings for the LC connector module.',
            'page callback'    => 'drupal_get_form',
            'page arguments'   => array('lc_connector_get_settings_form'),
            'access arguments' => array('administer users'),
            'file'             => 'lc_connector.admin.inc',
        ),
    ) + ((array) LCConnector_Handler::callSafely('Module', 'invokeHookMenu'));
}

/**
 * Modify admin area form for new widget
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_block_add_block_form_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Admin', 'alterWidgetModifyForm', array(&$form, &$form_state));
}

/**
 * Modify admin area form for existing widget
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_block_admin_configure_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Admin', 'alterWidgetModifyForm', array(&$form, &$form_state));
}

/**
 * Modify admin area form for widget delete confirmation
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_custom_block_delete_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Admin', 'alterWidgetDeleteForm', array(&$form, &$form_state));
}

/**
 * Change block definition before saving to the database 
 *
 * @param array  $blocks      A multidimensional array of blocks keyed by the defining module and delta
 * @param string $theme       The theme these blocks belong to
 * @param array  $code_blocks The blocks as defined in hook_block_info() before being overwritten by the database data
 *
 * @hook   block_info_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_block_info_alter(array &$blocks, $theme, array $code_blocks) {
    return LCConnector_Handler::callSafely('Admin', 'alterBlockInfo', array(&$blocks, $theme, $code_blocks));
}

/**
 * Modify block content
 *
 * @param array     &$data      Data returned from the hook_block_view()
 * @param \stdClass $form_state Block object loaded from the database
 *
 * @hook   block_view_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_block_view_alter(array &$data, \stdClass $block) {
    $settings = block_custom_block_get($block->delta);

    if (!empty($settings['lc_class']) && !LCConnector_Handler::callSafely('Block', 'setBlockContent', array(&$data, $block))) {
        $data['content'] = NULL;
    }
}

/**
 * Preprocess theme variables for a specific theme block
 *
 * @param array &$variables The variables array (modify in place)
 *
 * @hook   preprocess_block
 * @return void
 * @since  1.0.0
 */
function lc_connector_preprocess_block(array &$variables) {
    $settings = block_custom_block_get($variables['block']->delta);

    // Inject a CSS class for blocks generated by LiteCommerce
    if (!empty($settings['lc_class'])) {
        LCConnector_Handler::callSafely('Block', 'addCSSClass', array(&$variables, $settings['lc_class']));
    }
}

/**
 * A user account is about to be created or updated
 *
 * @param array     $edit     The array of form values submitted by the user
 * @param \stdClass $account  The user object on which the operation is performed
 * @param mixed     $category The active category of user information being edited
 *
 * @hook   user_presave
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_presave(array &$edit, \stdClass $account, $category) {
    return LCConnector_Handler::callSafely('Profile', 'performAction', array('presave', &$edit, $account, $category));
}

/**
 * A user account was created
 *
 * @param array     $edit     The array of form values submitted by the user
 * @param \stdClass $account  The user object on which the operation is performed
 * @param mixed     $category The active category of user information being edited
 *
 * @hook   user_insert
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_insert(array &$edit, \stdClass $account, $category) {
    $account->passwd = LCConnector_Handler::getVariable('passwd');

    return LCConnector_Handler::callSafely('Profile', 'performAction', array('insert', &$edit, $account, $category));
}

/**
 * A user account was updated
 *
 * @param array     $edit     The array of form values submitted by the user
 * @param \stdClass $account  The user object on which the operation is performed
 * @param mixed     $category The active category of user information being edited
 *
 * @hook   user_update
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_update(array &$edit, \stdClass $account, $category) {
    $account->passwd = LCConnector_Handler::getVariable('passwd');

    return LCConnector_Handler::callSafely('Profile', 'performAction', array('update', &$edit, $account, $category));
}

/**
 * A user account was cancelled
 *
 * @param array     $edit     The array of form values submitted by the user
 * @param \stdClass $account  The user object on which the operation is performed
 * @param mixed     $category The active category of user information being edited
 *
 * @hook   user_cancel
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_cancel(array $edit, \stdClass $account, $category) {
    return LCConnector_Handler::callSafely('Profile', 'performAction', array('cancel', &$edit, $account, $category));
}

/**
 * A user account was deleted
 *
 * @param \stdClass $account  The user object on which the operation is performed
 *
 * @hook   user_delete
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_delete($account) {
    $edit = array();

    return LCConnector_Handler::callSafely('Profile', 'performAction', array('delete', &$edit, $account, NULL));
}

/**
 * A role deleted
 *
 * @param \stdClass $role  The role object on which the operation is performed
 *
 * @hook   user_role_delete
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_role_delete($role) {
    $roles = array($role);

    return LCConnector_Handler::callSafely('Profile', 'performAction', array('deleteRole', &$roles, new \stdClass(), NULL));
}

/**
 * The user just logged in
 *
 * @param array     $edit     The array of form values submitted by the user
 * @param \stdClass $account  The user object on which the operation is performed
 * @param mixed     $category The active category of user information being edited
 *
 * @hook   user_login
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_login(array &$edit, \stdClass $account, $category) {
    return LCConnector_Handler::callSafely('Profile', 'performAction', array('login', &$edit, $account, $category));
}

/**
 * The user just logged out
 *
 * @param \stdClass $account The user object on which the operation is performed
 *
 * @hook   user_logout
 * @return void
 * @since  1.0.0
 */
function lc_connector_user_logout(\stdClass $account) {
    $edit = array();

    return LCConnector_Handler::callSafely('Profile', 'performAction', array('logout', &$edit, $account, NULL));
}

/**
 * Modify login form
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_user_login_block_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Customer', 'alterLoginForm', array(&$form, &$form_state));
}

/**
 * Perform necessary alterations to the JavaScript before it is presented on the page
 *
 * @param array &$javascript An array of all JavaScript being presented on the page
 *
 * @hook   js_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_js_alter(array &$javascript) {
    return LCConnector_Handler::callSafely('Module', 'optimizeJSFiles', array(&$javascript));
}

/**
 * Perform necessary alterations to the CSS before it is presented on the page
 *
 * @param array &$css An array of all CSS being presented on the page
 *
 * @hook   css_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_css_alter(array &$css) {
    return LCConnector_Handler::callSafely('Module', 'optimizeCSSFiles', array(&$css));
}

/**
 * Alters outbound URLs
 *
 * @param string &$path         The outbound path to alter
 * @param array  &$options      A set of URL options
 * @param string $original_path The original path, before being altered by any modules
 *
 * @hook   url_outbound_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_url_outbound_alter(&$path, array &$options, $original_path) {
    LCConnector_Handler::callSafely('Module', 'translateOutboundURL', array(&$path, &$options, $original_path));
}

/**
 * Alters inbound URLs
 *
 * @param string &$path         Path
 * @param string $original_path Original path
 * @param string $path_language Path language
 *
 * @hook   url_inbound_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_url_inbound_alter(&$path, $original_path, $path_language) {
    LCConnector_Handler::callSafely('Module', 'translateInboundURL', array(&$path, $original_path, $path_language));
}

/**
 * Process cron tasks
 *
 * @hook   cron
 * @return void
 * @since  1.0.0
 */
function lc_connector_cron() {
    LCConnector_Handler::callSafely('Module', 'runCronTasks');
}

/**
 * Returns lc_connector specific permissions
 *
 * @hook   permission
 * @return array
 * @since  1.0.0
 */
function lc_connector_permission() {
    return array('lc admin' => array('title' => t('LiteCommerce administrator')));
}

/**
 * Add additional processor on submit to the form
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_user_profile_form_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Admin', 'alterUserProfileForm', array(&$form, &$form_state));
}

/**
 * Add additional processor on submit to the form
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_user_register_form_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Admin', 'alterUserRegisterForm', array(&$form, &$form_state));
}

/**
 * Update variables array passed to the template
 *
 * @param array &$variables Array of variables
 *
 * @hook   template_process_page
 * @return void
 * @since  1.0.0
 */
function lc_connector_process_page(array &$variables) {
    return LCConnector_Handler::callSafely('Controller', 'updateTemplateVars', array(&$variables));
}

/**
 * Update meta tags array passed to the template
 *
 * @param array &$elements Array of meta tags
 *
 * @hook   html_head_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_html_head_alter(array &$elements) {
    LCConnector_Handler::callSafely('Controller', 'updateMetaTags', array(&$elements));
}

/**
 * Add additional processor on submit to the form
 *
 * @param array &$form       Form description
 * @param array &$form_state Form state
 *
 * @hook   form_FORM_ID_alter
 * @return void
 * @since  1.0.0
 */
function lc_connector_form_user_admin_permissions_alter(array &$form, array &$form_state) {
    return LCConnector_Handler::callSafely('Admin', 'alterUserPermissionsForm', array(&$form, &$form_state));
}

/**
 * Check access for current LC controller
 *
 * FIXME: Drupal calls this function before the module is initialized.
 * So, we cannot move it into the "Include/Callbacks.php".
 * See: http://drupal.org/node/928160
 *
 * TODO: check if we can call LC methods here.
 * Or the initialization is not required.
 * Or it's not needed to check anything here at all
 *
 * @return boolean
 * @since  1.0.0
 */
function lc_connector_check_controller_access() {
    return is_null($result = LCConnector_Handler::callSafely('Controller', 'checkAccess')) ? TRUE : $result;
}
